package com.qeeyou.apps.android.craftsman.activity.home;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.qeeyou.apps.android.craftsman.R;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuchengxiong on 2018/2/6.
 */

public class TabLayoutFragment extends Fragment {
    public static final String TABLAYOUT_FRAGMENT = "tab_fragment";
    private GridView gridView;
    private MyGridViewAdapter mAdapter;
    private List<String> timeList = new ArrayList<String>();
    private int type;

    public static TabLayoutFragment newInstance(int type) {
        TabLayoutFragment fragment = new TabLayoutFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(TABLAYOUT_FRAGMENT, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getArguments() != null) {
            type = (int)getArguments().getSerializable(TABLAYOUT_FRAGMENT);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_scheduling, container, false);
        initView(view);
        return view;
    }

    protected void initView(View view) {
        gridView = (GridView) view.findViewById(R.id.gridView4Scheduling);
        String[] timeArray = this.getActivity().getResources().getStringArray(R.array.schedule_time);
        timeList.clear();
        for(int i = 0, n = timeArray.length; i < n; i++) {
            String timeStr = timeArray[i];
            timeList.add(timeStr);
            timeList.add(timeStr);
            timeList.add(timeStr);
        }
        mAdapter = new MyGridViewAdapter(this.getActivity().getApplicationContext(), timeList);
        gridView.setAdapter(mAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(TabLayoutFragment.this.getActivity().getApplicationContext(),
                        "你点击了第" + (position + 1) + "个Item", Toast.LENGTH_SHORT ).show();
            }
        });

    }


}
